package resolver

import "strings"

type RecordType int8

const (
	A RecordType = iota
	AAAA
	NS
	CNAME
	MX
	TXT
)

var RecordTypeMap = map[RecordType]string{
	A:     "A",
	AAAA:  "AAAA",
	NS:    "NS",
	CNAME: "CNAME",
	MX:    "MX",
	TXT:   "TXT",
}

// 打印对应状态的时候，转换为对应的字符串
func (s RecordType) String() string {
	v, ok := RecordTypeMap[s]
	if ok {
		return v
	}
	return ""
}

func GetType(rt string) (RecordType, bool) {
	rt = strings.ToUpper(rt)
	switch rt {
	case "A":
		return A, true
	case "AAAA":
		return AAAA, true
	case "NS":
		return NS, true
	case "CNAME":
		return CNAME, true
	case "MX":
		return MX, true
	case "TXT":
		return TXT, true
	default:
		return A, false
	}
}

func GetsType(rts []string) (RecordType, bool) {
	for _, rt := range rts {
		rt = strings.ToUpper(rt)
		switch rt {
		case "A":
			return A, true
		case "AAAA":
			return AAAA, true
		case "NS":
			return NS, true
		case "CNAME":
			return CNAME, true
		case "MX":
			return MX, true
		case "TXT":
			return TXT, true
		default:
			return A, false
		}
	}
	return A, false
}
